package com.zhihuichongcheng.application.ui.cricle.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.cricle.CricleTypeBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.cricle.adapter.CircleTitleTypeAdapter;
import com.zhihuichongcheng.application.ui.cricle.adapter.HealthyAdapter;
import com.zhihuichongcheng.application.ui.cricle.fragment.CircleHealthyFragment;
import com.zhihuichongcheng.application.util.FullyLinearLayoutManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * Created by 3water on 2016/10/28.
 */
public class CircleHealthyActivity extends BaseActivity {
    private ViewPager mVpHealthy;
    private RecyclerView mRcvTitle;
    private HealthyAdapter mAdapter = null;
    private List<Fragment> mFragments = new ArrayList<>();
    private String mTitle, mType;
    private CircleTitleTypeAdapter mCircleTitleTypeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cricle_healthy);
        initView();
        initData();
    }

    private void initView() {
        mRcvTitle = (RecyclerView) findViewById(R.id.rcv_title);
        FullyLinearLayoutManager manager = new FullyLinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mCircleTitleTypeAdapter = new CircleTitleTypeAdapter(mContext);
        mRcvTitle.setLayoutManager(manager);
        mRcvTitle.setAdapter(mCircleTitleTypeAdapter);
        setImRight(R.drawable.ic_add_cricle_press, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(AddCricleActivity.class, 0, null);
            }
        });
        mVpHealthy = (ViewPager) findViewById(R.id.vp_healthy);
    }

    private void initData() {
        DialogUtil.showLoadDiadlog(mContext);
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("title")) {
            mTitle = intent.getStringExtra("title");
        }
        setTitle(mTitle);
        if (intent != null && intent.hasExtra("type")) {
            mType = intent.getStringExtra("type");
        }
        loadCategoryId();
    }

    private void loadCategoryId() {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", mUser.getToken());
        map.put("parentId", mType);
        mManager.post(NetworkAPI.CricleCategorys, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(mContext);
                List<CricleTypeBean> mListType = GsonUtil.paraeFromStringToList(value, CricleTypeBean.class);
                if (mListType.size() == 0) {
                    mRcvTitle.setVisibility(View.GONE);
                }
                CricleTypeBean bean = new CricleTypeBean();
                bean.setJinbaoHomeCategoryId(mType);
                bean.setCategoryName("全部");
                mListType.add(0, bean);
                mCircleTitleTypeAdapter.setData(mListType);
                mCircleTitleTypeAdapter.setIndex(0);
                mCircleTitleTypeAdapter.notifyDataSetChanged();
                mCircleTitleTypeAdapter.setOnItemOnclick(new OnItemOnclick<CricleTypeBean>() {
                    @Override
                    public void onClick(CricleTypeBean cricleTypeBean, int position) {
                        mVpHealthy.setCurrentItem(position, false);
                        mCircleTitleTypeAdapter.setIndex(position);
                        mCircleTitleTypeAdapter.notifyDataSetChanged();
                    }
                });
                for (int i = 0; i < mListType.size(); i++) {
                    CricleTypeBean cricleTypeBean = mListType.get(i);
                    CircleHealthyFragment fragment = new CircleHealthyFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("type", cricleTypeBean.getJinbaoHomeCategoryId());
                    bundle.putString("title", cricleTypeBean.getCategoryName());
                    fragment.setArguments(bundle);
                    mFragments.add(fragment);
                }
                mVpHealthy.setOffscreenPageLimit(mListType.size());
                mAdapter = new HealthyAdapter(getSupportFragmentManager(), mFragments);

                mVpHealthy.setAdapter(mAdapter);
                mVpHealthy.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        mCircleTitleTypeAdapter.setIndex(position);
                        mCircleTitleTypeAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            }

            @Override
            public void onFailure(String value) {
                DialogUtil.dismiss(mContext);
            }
        }, map);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            int index = mVpHealthy.getCurrentItem();
            CircleHealthyFragment fragment = (CircleHealthyFragment) mFragments.get(index);
            fragment.reflush();
        }
    }
}
